﻿//---------------------------------------------------------------
// Copyright (C) 2007 Microsoft Corporation. All rights reserved.
//---------------------------------------------------------------
var myContactsEl = null;
function pageLoad1() 
{
    if( myContactsEl != null ) return;
    myContactsEl = document.getElementById("ContactsControl");
    var photoEl = document.getElementById("PhotoControl");
	photoEl.setAttribute("devlive:onSignin", "");
	photoEl.setAttribute("devlive:onSignout", "");
    myContactsEl.setAttribute("devlive:onData", "");
	myContactsEl.setAttribute("devlive:onSignin", "");
	myContactsEl.setAttribute("devlive:onSignout", "");
    // Assign default values to the generated proxy.
    RoadTripEx.RouteService.set_defaultUserContext("Default context");
    RoadTripEx.RouteService.set_defaultSucceededCallback(SucceededCallback);
    RoadTripEx.RouteService.set_defaultFailedCallback(FailedCallback);
    qs();
    if( document.getElementById('EmailID').value.length < 2 )
    {
        alert('User ID not found');
    }
    else 
    {
        onGetRouteList( document.getElementById('EmailID').value );
    }
    Splash.initalize("mySplash");
 }



function qs()
{
    document.getElementById('EmailID').value = '';
    var query = window.location.search.substring(1);
    var parms = query.split('&');
    if( parms.length < 1 ) return;
    var pos = parms[0].indexOf('UID=');
    if (pos > -1) 
    {
        document.getElementById('EmailID').value = parms[0].substring(pos+4);
    }
} 



function onGetRouteList( userid )
{
    RoadTripEx.RouteService.GetRouteNameList( userid);
}

function onChangeRoute()
{
    var userid = document.getElementById('EmailID').value;
    var rl = document.getElementById('RouteList');
    if( rl.options[rl.selectedIndex].value == '0' )
    {
        SetCreateMode();
        return;
    }
    RoadTripEx.RouteService.GetRouteList( userid, rl.options[rl.selectedIndex].value);
}

function onSaveRoute()
{
    RoadTripEx.RouteService.UpdateRoute( document.getElementById('EmailID').value, GetCurrentRoute());
}

function onDeleteRoute()
{
    RoadTripEx.RouteService.RemoveRoute( document.getElementById('EmailID').value, document.getElementById('RouteID').value);
}

function onEmailRoute()
{
    if( document.getElementById('RouteID').value.length < 2 )
    {
        alert('Please save first before email');return;
    }
    if( _Error == true )
    {
        CollectEmail();
        return;
    }
    
    var myContactsEl = document.getElementById("ContactsControl");
    document.getElementById('ContactsControl').style.visibility = "visible";
    //document.getElementById('RouteView').style.visibility = "hidden";
    //document.getElementById('RouteView2').style.visibility = "visible";
    myContactsEl.setAttribute("devlive:onData", "onData");
}

function CollectEmail()
{
    alert('Sorry. The Windows Live Contacts service is down. Please Enter the Emails manually.');
    var url, mailto, mtUrl, msg;
	var s2= document.getElementById('RouteTitle').value ;
	var s = '<h3> From : '+ document.getElementById('StartLocation').value + '</br> To : ' + document.getElementById('EndLocation').value +' </h3>  </br>';
	s += 'Click each link below to email the personalized driving directions to your friends. </br> \n';
	mailto = "mailto:?subject={0}&body={1}";
	url = "http://" + window.location.host + '/RoadTrip/RoadTripViewer.aspx?IID=' + document.getElementById('RouteID').value;

	msg = 'User Name,\n\n I would like to share my pictures taken during the trip Click on the URL below for personalized driving directions  \n' + url;
	mtUrl = mailto.format(s2, msg );
	s += ('<div class="routeName"><b>{1}</b> <a href="{0}" title= Road Trip > Email </a> <a target="_blank" href="{2}" title= See what they will see> Preview</a></div>').format(mtUrl, 'User Name', url);
	Splash.show('<h1>'+ s2 + '</h1>', s, false,true );
}


function onShowPhotoControl()
{
    if( _Error == true )
    {
        alert('Sorry. The Windows Live Photo service is down. Please Enter the Photo URLS manually.');
        return;
    }
    document.getElementById('PhotoControl').style.visibility = 'visible';
}

function GetRouteNameListCallback(result)
{
    var routelist = document.getElementById('RouteList');
    routelist.options.length = 1;
    routelist.options[0].text = 'Select Route';
    routelist.options[0].value = '0';
       
    if( result == null )
    {
        Splash.show('Welcome New User', '<p>This sample "mash up" application highlights three cool Microsoft technologies available to third party developers:</p> ' +			'<p><span class="nowrap">Virtual Earth&trade;</span><span class="nowrap">, Window Live ID&trade;</span> and <span class="nowrap">Windows Live Contacts&trade;.</span></p>' +			'<p><span class="nowrap">Please press new to create a new Route</span></p>' +			'<p><img src="images/animated_loading.gif" style="width:16px; height:16px; vertical-align:middle;padding-right:5px;" alt=loading... /> Loading contacts...</p>',5);
    
        return;
    }
    for(var item in result)
    {  
        var optn = document.createElement("OPTION");                
        optn.text = result[item].Name;
        optn.value = result[item].Id;
        routelist.options.add(optn);
    }
     Splash.show('Welcome Existing User', '<p>This sample "mash up" application highlights three cool Microsoft technologies available to third party developers:</p> ' +			'<p><span class="nowrap">Virtual Earth&trade;</span><span class="nowrap">, Window Live ID&trade;</span> and <span class="nowrap">Windows Live Contacts&trade;.</span></p>' +			'<p><span class="nowrap">Please select the routes to modify/delete or press new to create a new Route</span></p>' +			'<p><img src="images/animated_loading.gif" style="width:16px; height:16px; vertical-align:middle;padding-right:5px;" alt=loading... /> Loading contacts...</p>',5);

     
}

function GetRouteListCallback(result)
{
    PopulateMap(result);
}

function UpdateRouteCallback(result)
{
    var routetitle = document.getElementById('RouteTitle');
    document.getElementById('RouteID').value = result;
    alert(  routetitle.value + ' successfuly saved ');
}

function RemoveRouteCallback(result)
{
    document.getElementById('RouteID').value = '';
    alert(  document.getElementById('RouteTitle').value + ' successfuly removed');
    window.location.reload( false );
}

// Callback function invoked when the call to 
// the Web service methods succeeds. 
function SucceededCallback(result, userContext, methodName)
{ 
   var message;
    switch(methodName)
    {
        case ("GetRouteNameList"):
        {
            GetRouteNameListCallback(result);
            break;
        }
      
        case ("GetRouteList"):
        {
            GetRouteListCallback(result);
            break;
        }
      
        case ("UpdateRoute"):
        {
            UpdateRouteCallback(result);
            break;
        }
        
        case ("RemoveRoute"):
        {
            RemoveRouteCallback(result);
            break;
        }
        
        default:
        {
        }
    }       

}

// Callback function invoked when the call to 
// the Web service methods fails.
function FailedCallback(error, userContext, methodName) 
{
    if(error !== null) alert('An error occurred: ' + error.get_message());
}


if (typeof(Sys) !== "undefined") Sys.Application.notifyScriptLoaded();
